﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics.Contracts;

namespace RaisingForce.Raise.Language.Model
{
    public abstract class Word
    {
        #region Constructors

        protected Word(
            WordType wordType,
            Glyph primaryGlyph,
            Glyph secondaryGlyph)
        {
            if (wordType == null)
            {
                throw new ArgumentNullException("wordType");
            }
            if (primaryGlyph == null)
            {
                throw new ArgumentNullException("primaryGlyph");
            }
            if (primaryGlyph.GetType() != wordType.PrimaryGlyphType)
            {
                throw new ArgumentException("primaryGlyph");
            }
            if (secondaryGlyph != null
                && secondaryGlyph.GetType() != wordType.SecondaryGlyphType)
            {
                throw new ArgumentException("secondaryGlyph");
            }
            Contract.EndContractBlock();
            this.wordType = wordType;
            this.primaryGlyph = primaryGlyph;
            this.secondaryGlyph = secondaryGlyph;
        }

        #endregion

        #region Private Declarations

        private readonly WordType wordType;

        private readonly Glyph primaryGlyph;

        private readonly Glyph secondaryGlyph;

        [ContractInvariantMethod]
        private void ObjectInvariant()
        {
            Contract.Invariant(this.wordType != null);
            Contract.Invariant(this.primaryGlyph != null && this.primaryGlyph.GetType() == this.wordType.PrimaryGlyphType);
            Contract.Invariant(this.secondaryGlyph == null || this.secondaryGlyph.GetType() == this.wordType.SecondaryGlyphType);
        }

        #endregion

        #region Properties

        public WordType WordType
        {
            get
            {
                Contract.Ensures(Contract.Result<WordType>() != null);
                return this.wordType;
            }
        }

        public Glyph PrimaryGlyph
        {
            get
            {
                Contract.Ensures(Contract.Result<Glyph>() != null && Contract.Result<Glyph>().GetType() == this.WordType.PrimaryGlyphType);
                return this.primaryGlyph;
            }
        }

        public Glyph SecondaryGlyph
        {
            get
            {
                Contract.Ensures(Contract.Result<Glyph>() == null || Contract.Result<Glyph>().GetType() == this.WordType.SecondaryGlyphType);
                return this.secondaryGlyph;
            }
        }

        #endregion

        #region Public Methods

        public WordData GetData()
        {
            Contract.Ensures(Contract.Result<WordData>() != null);
            return new WordData(
                this.WordType.Name,
                this.PrimaryGlyph.TypeName,
                this.PrimaryGlyph.Value,
                this.SecondaryGlyph != null ? this.SecondaryGlyph.TypeName : null,
                this.SecondaryGlyph != null ? this.SecondaryGlyph.Value : null);
        }

        #endregion

        #region Static Methods

        [Pure]
        public static bool TypeIsAllowed(Type wordType)
        {
            return wordType == typeof(IfWord)
                || wordType == typeof(ThenWord)
                || wordType == typeof(ChainWord);
        }

        public static T Create<T>(
            WordType wordType,
            Glyph primaryGlyph,
            Glyph secondaryGlyph = null)
            where T : Word
        {
            Contract.Requires(wordType != null);
            Contract.Requires(primaryGlyph != null && primaryGlyph.GetType() == wordType.PrimaryGlyphType);
            Contract.Requires(secondaryGlyph == null || secondaryGlyph.GetType() == wordType.SecondaryGlyphType);
            Contract.Requires(Word.TypeIsAllowed(typeof(T)));
            Word creatingWord;
            if (typeof(T) == typeof(IfWord))
            {
                creatingWord =
                    new IfWord((IfWordType)wordType, primaryGlyph, secondaryGlyph);
            }
            else if (typeof(T) == typeof(ThenWord))
            {
                creatingWord =
                    new ThenWord((ThenWordType)wordType, primaryGlyph, secondaryGlyph);
            }
            else if (typeof(T) == typeof(ChainWord))
            {
                creatingWord =
                    new ChainWord((ChainWordType)wordType, primaryGlyph);
            }
            else
            {
                throw new InvalidOperationException(
                    string.Format(
                        "Unknown Word type found: {0}.",
                        typeof(T).FullName));
            }
            return (T)creatingWord;
        }

        #endregion
    }
}
